﻿//创建时间: 2010-10-15
//创建作者: 李晓忠
//功能说明: CMS系统 结点附加发布管理
//最后修改: 2010-10-15
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.Admin
{
    public partial class NodeAddionalPub_save : BaseAdmin
    {
        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.Node_Save;
        }
        #endregion

        #region 页面变量
        protected bool m_IsAdd = false;
        protected string m_strTitle = "结点附加发布管理";
        protected int m_Id = 0;
        protected int m_intNodeId = 0;
        NodeAddionalPubManage oNodeAddionalPubManage = new NodeAddionalPubManage();
        NodeAddionalPubInfo oNodeAddionalPubInfo = new NodeAddionalPubInfo();
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            m_Id = RequestHelper.RequestInt("id", 0);
            m_intNodeId = RequestHelper.RequestInt("nodeid",0);
            if (m_intNodeId > 0)//验证用户对结点的权限
                base.AlertForNoPermission(base.CheckIsHavePermission(m_intNodeId));
            if (!IsPostBack)
            {
                LoadBase();
                LoadData();
            }
        }
        protected void lbtnDelete_Click(object sender, EventArgs e)
        {
            LinkButton lbtn = (LinkButton)sender;
            int intFormInputLimitTypeId = 0;
            int.TryParse(lbtn.CommandArgument, out intFormInputLimitTypeId);
            SubmitAlertDelete(oNodeAddionalPubManage.DeleteInfoById(intFormInputLimitTypeId));
            LoadBase();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            #region 验证
            if (txtPubName.Text == "")
            {
                Alert("发布名不能为空"); return;
            }
            if (txtPageName.Text == "")
            {
                Alert("发布文件名不能为空"); return;
            }
            else
            {
                if (!CommonRegex.MatchExtFile(txtPageName.Text).Success)
                {
                    Alert(" 请填写发布文件名后缀"); return;
                }
            }
            if (txtTemplate.Text == "")
            {
                Alert("套用模板不能为空"); return;
            }

            #endregion

            #region 赋值
            oNodeAddionalPubInfo = (NodeAddionalPubInfo)GetPageViewState("model");
            if (oNodeAddionalPubInfo != null)
            {
                oNodeAddionalPubInfo.UpdateTime = GetAdminAddTime;
                oNodeAddionalPubInfo.UpdateUserId = base.mUserBaseInfo.UserBaseId;
            }
            else
            {
                oNodeAddionalPubInfo = new NodeAddionalPubInfo();
                oNodeAddionalPubInfo.AddTime = GetAdminAddTime;
                oNodeAddionalPubInfo.AddUserId = base.mUserBaseInfo.UserBaseId;
                oNodeAddionalPubInfo.UpdateTime = GetAdminAddTime;
                oNodeAddionalPubInfo.UpdateUserId = base.mUserBaseInfo.UserBaseId;
                oNodeAddionalPubInfo.NodeId = m_intNodeId;
            }

            oNodeAddionalPubInfo.Root ="";
            oNodeAddionalPubInfo.Template = ("/" + txtTemplateRoot.Value + "/" + txtTemplate.TextFilterSQL).Replace("//", "/");
            oNodeAddionalPubInfo.PageName = txtPageName.TextFilterSQL;
            oNodeAddionalPubInfo.PubName = txtPubName.TextFilterSQL;
            oNodeAddionalPubInfo.SelfPSN= txtSelfPSN.TextFilterSQL;
            oNodeAddionalPubInfo.SelfPSNURL = txtSelfUrl.TextFilterSQL;
            oNodeAddionalPubInfo.Intro = txtIntro.TextFilterSQL;

            if (oNodeAddionalPubInfo.SelfPSNURL != "")
            {
                oNodeAddionalPubInfo.Url = (new PSNManage()).GetPSNURLByPSNUrl(oNodeAddionalPubInfo.SelfPSNURL) + "/" + oNodeAddionalPubInfo.PageName;
            }
            else
            {
                NodeInfo m_NodeInfo = (new NodeManage()).GetInfoById(oNodeAddionalPubInfo.NodeId);
                if (m_NodeInfo != null)
                {
                    if (m_NodeInfo.ContentPubTypeId == (int)ContentPubType.EnumElement.StaticPub)
                    {
                        if (m_NodeInfo.HtmlUrl != "")
                        {
                            oNodeAddionalPubInfo.Url = (new PSNManage()).GetPSNURLByPSNUrl(m_NodeInfo.HtmlUrl) + "/" + oNodeAddionalPubInfo.PageName;
                        }
                        else
                        {
                            if (SYSConfig.SystemVar_DefaultContentPSNURL != "")
                            {
                                oNodeAddionalPubInfo.Url = (new PSNManage()).GetPSNURLByPSNUrl(SYSConfig.SystemVar_DefaultContentPSNURL) + "/" + oNodeAddionalPubInfo.PageName;
                            }
                            else
                            {
                                Alert("系统没有默认的发布URL");
                            }
                        }
                    }
                }
            }
            #endregion

            SubmitAlertAndThisNewLocation(oNodeAddionalPubManage.Save(oNodeAddionalPubInfo), "NodeAddionalPub_list.aspx?nodeid=" + oNodeAddionalPubInfo.NodeId);
            
          //  LoadBase();
        }
        #endregion

        #region 页面方法
        public void LoadBase()
        {

           
        }
        public void LoadData()
        {
            if (m_Id > 0)
            {
                oNodeAddionalPubInfo = oNodeAddionalPubManage.GetInfoById(m_Id);
                m_IsAdd = false;
                if (oNodeAddionalPubInfo != null)
                {
                    InitPageViewState("model", oNodeAddionalPubInfo);
                   //txtRootPSN.Text = oNodeAddionalPubInfo.Root;
                    string strTemp = oNodeAddionalPubInfo.Template;
                    if (strTemp != "")
                    {
                      string[] arr=  strTemp.Split('/');
                      for (int i = 0; i < arr.Length; i++)
                      {
                          if (arr[i] != "")
                          {
                              txtTemplateRoot.Value = arr[i];
                              strTemp = strTemp.Replace(arr[i],"").Replace("//","/");
                              break;
                          }
                      }
                    }
                    txtTemplate.Text = strTemp;
                    txtPubName.Text = oNodeAddionalPubInfo.PubName;
                    txtPageName.Text = oNodeAddionalPubInfo.PageName;
                    txtSelfPSN.Text = oNodeAddionalPubInfo.SelfPSN;
                    txtSelfUrl.Text = oNodeAddionalPubInfo.SelfPSNURL;
                    span_edit_content.Visible = true;
                    m_intNodeId = oNodeAddionalPubInfo.NodeId;
                    txtIntro.Text = oNodeAddionalPubInfo.Intro;
                }
            }
            else if (m_intNodeId > 0)
            {
                m_IsAdd = true;
                txtPageName.Text=(oNodeAddionalPubManage.GetMaxId() + 1).ToString() + ".html";
            }
            else
            {
                Alert("请求参数有误"); return;
            }
        }
        #endregion
    }
}
